
package com.scs.application.modules.upms.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.exceptions.BusinessException;
import com.scs.application.core.model.*;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.base.entity.HospitalCatalog;
import com.scs.application.modules.upms.entity.Area;
import com.scs.application.modules.upms.request.AreaQueryRequest;
import com.scs.application.modules.upms.service.AreaService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
* 行政区划 controller
*
*/
@AllArgsConstructor
@RestController
@RequestMapping("/upms/areas")
public class AreaController extends CrudController<Area,String, AreaService> implements ImAndExAbleController<AreaQueryRequest> {

    private final  AreaService service;


    @GetMapping("/page")
    public PageResult page(AreaQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(AreaQueryRequest request) {
        List<Area> list = service.list(Wrappers.<Area>query().orderByDesc("gmt_modified"));
        return CommonResult.ok("查询成功", list);
    }

    @GetMapping("/tree")
    public CommonResult tree(AreaQueryRequest request) {
        List<Area> list = service.list(request);
        TreeNode root = null;
        if(request.isAutoAddRoot()){
            root = new TreeNode();
            root.setId("0");
            root.setTitle("行政区划");
        }
        TreeModel<Area> treeModel = new TreeModel<Area>(list, "name", root);
        return CommonResult.ok("查询成功", treeModel.asList());
    }



    /***
     * @Description:  通用删除，多个
     */
    @DeleteMapping("/deleteBatchCommon")
    public RestResult deleteBatchCommon(@RequestBody Map<String, Object> para) {

        List<Serializable> ids = (List<Serializable>) para.get("ids");
        //是否有下下级
        int count = service.count(Wrappers.<Area>query().in("parent_id", ids));
        if (count > 0) {
            throw new BusinessException("存在下级，不允许删除");
        }
        Boolean isOk = service.removeByIds(ids);
        if (!isOk) {
            return RestResult.error("数据删除失败<br>可能所操作数据已被删除，请刷新页面重新尝试操作");
        }
        return RestResult.ok("数据删除成功");
    }

    /***
     * @param id
     * @Description:  通用查找，单个
     */
    @GetMapping("/findByIdCommon")
    public RestResult findByIdCommon(@RequestParam("id") Serializable id) {
        Area entity = service.getById(id);
        if (entity == null) {
            return RestResult.error("数据查询失败<br>可能所操作数据已被删除，请刷新后重新尝试操作");
        }
        if (StringUtils.isNotBlank(entity.getParentId()) && !"0".equals(entity.getParentId())) {
            Area entityParent = service.getById(entity.getParentId());
            if (entityParent != null) {
                entity.setParentName(entityParent.getName()).setParentId(entityParent.getId());
            }
        } else{
            entity.setParentName("行政区划").setParentId("0");
        }

        return RestResult.ok("获取数据成功", entity);
    }



}
